﻿using System;
using System.Collections.Generic;

using Castle.MonoRail.Framework;
using Castle.MonoRail.Framework.Helpers;
using Castle.MonoRail.ViewComponents;

using StateTheater.Models;

namespace StateTheater.Component
{
	[ViewComponentDetails("SeasonList")]
	public class SeasonListViewComponent  : ViewComponentEx
	{
		[ViewComponentParam("source", Required = true)]
		public IList<ShowLine> Source { get; set; }

		public override void Render()
		{
			base.Render();
			string siteRoot = Context.ContextVars["siteRoot"] as string;
			IEnumerator<ShowLine> iter = Source.GetEnumerator();
			RenderText("\r\n<ul class=\"season\">\r\n");
			bool contin = iter.MoveNext();
			while (contin)
			{
				ShowLine show = iter.Current as ShowLine;
				if (show.FlowerID > 0)
				{
					RenderText("<ul class=\"season\">\r\n");
					do
					{
						RenderTextFormat("\t<li><a class=\"show innershow\" href=\"{0}/show/{1}.rails\">{2}</a></li>\r\n",
							siteRoot, 
							show.ProductionID,
							Util.AdjTitle(show.Title));

						contin = iter.MoveNext();
						show = iter.Current as ShowLine;
					} while (contin && show.FlowerID > 0);
					RenderText("</ul>\r\n");
				}
				if (show != null)
					RenderTextFormat("\t\t<li><span class=\"show\"><a href=\"{0}/show/{1}.rails\">{2}</a></span>\r\n\t\t\t<span class=\"dates\">{3} &ndash; {4}</span></li>\r\n",
						siteRoot,
						show.ProductionID,
						Util.AdjTitle(show.Title),
						show.FirstPerformance.ToShortDateString(),
						show.LastPerformance.ToShortDateString());

				contin = iter.MoveNext();

			}
			RenderText("</ul>\r\n");
			CancelView();
		}

	}

	public class AllShowsComponent : ViewComponent
	{
		[ViewComponentParam("source", Required = true)]
		public IList<ShowLine> Source { get; set; }

		[ViewComponentParam("start", Required = true)]
		public int SeasonStart { get; set; }

		[ViewComponentParam("end", Required = true)]
		public int SeasonEnd { get; set; }
		private static readonly Comparison<ShowLine> ByDate = (ShowLine p1, ShowLine p2) =>
			{
				int result = p1.FirstPerformance.CompareTo(p2.FirstPerformance);
				if (result == 0) result = p1.FlowerID.CompareTo(p2.FlowerID);

				return result;
			};

		public override void Render()
		{
			List<ShowLine> season = new List<ShowLine>();
			base.Render();
			ShowLine first = null;
			TimeSpan oneyear = new TimeSpan(365, 0, 0, 0);
			DateTime ThisSeason = DateTime.MaxValue;
			foreach (ShowLine sl in Source)
			{
				if (first == null)
				{
					first = sl;
					if (first.FirstPerformance.Month >= SeasonStart)
						ThisSeason = new DateTime(first.FirstPerformance.Year, SeasonStart, 1);
					else
						ThisSeason = new DateTime(first.FirstPerformance.Year-1, SeasonStart, 1);
				}
				if (sl.FirstPerformance < ThisSeason)
				{
					RenderOneSeason(season, ThisSeason);
					season.Clear();
					first = null;
				}
				season.Add(sl);

			}
			if (season.Count > 0)
				RenderOneSeason(season, ThisSeason);
			CancelView();

		}

		void RenderOneSeason(List<ShowLine> season, DateTime ThisSeason)
		{
			if (SeasonStart == 1)
				RenderText(String.Format("<div class=\"seasonHeader\">{0}</div>", ThisSeason.Year));
			else
				RenderText(String.Format("<div class=\"seasonHeader\">{0} - {1}</div>", ThisSeason.Year, ThisSeason.Year + 1));

			SeasonListViewComponent seasonlist = new SeasonListViewComponent();
			seasonlist.Init(this.EngineContext, this.Context);
			season.Sort(ByDate);
			seasonlist.Source = season;
			seasonlist.Render();
		}
	}
}
